#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Forms.Skins.Resources;
using Gizmox.WebGUI.Common.Resources;
using System.Text.RegularExpressions;

#endregion

namespace VisualWebGuiApplication1
{
    public partial class GridEXFilterControl : UserControl
    {
        GridEX _gridEX;
        public delegate void FilterChangeHandle(object sender, string args);
        public event FilterChangeHandle OnFilterChange;
        const string _EQ = "=";
        const string _BE = "*|";
        const string _EN = "|*";
        const string _CO = "*|*";
        const string _G = ">";
        const string _L = "<";
        const string _GE = ">=";
        const string _LE = "<=";
        const string _EM = "!";
        [PropertyTab]
        public GridEX GridEX
        {
            get { return _gridEX; }
            set { _gridEX = value; }
        }
        public GridEXFilterControl()
        {
            InitializeComponent();
            //Search.SelectedIndex = 0;
        }

        public void Rebind()
        {
            if (_gridEX != null && Field.Items.Count == 0)
            {
                Field.Items.Clear();
                int left = 0;
                foreach (ColumnHeader col in _gridEX.Columns)
                {
                    clsFilterItem it = new clsFilterItem();
                    it.Code = col.Label;
                    it.Description = col.Text;
                    it.Datatype = col.Type.ToString();
                    Field.Items.Add(it);
                    //PictureBox pic = new PictureBox();
                    //pic.Image = new ImageResourceHandle("search.png");
                    //pic.Click += new EventHandler(pic_Click);
                    //pic.Width = pic.Height = 21;
                    //TextBox tex = new TextBox();
                    //tex.Name = col.Name;
                    //tex.Width = col.Width - pic.Width - 2 > 0 ? col.Width - pic.Width - 2 : 0;
                    //tex.Left = left;
                    //tex.Top = pic.Top = 0;
                    //pic.Left = tex.Left + tex.Width + 2;

                    //this.Controls.Add(tex);
                    //this.Controls.Add(pic);
                    //left += tex.Width + pic.Width + 5;
                }
                Field.DisplayMember = "Description";
                Search.SelectedIndex = 0;
                Field.SelectedIndex = 0;
            }
            
        }


        void pic_Click(object sender, EventArgs e)
        {
            if (OnFilterChange != null)
                OnFilterChange(this, "OnClick");
        }

        private void button1_Click(object sender, EventArgs e)
        {


        }
        class clsFilterItem
        {
            string _code;

            public string Code
            {
                get { return _code; }
                set { _code = value; }
            }
            string _description;

            public string Description
            {
                get { return _description; }
                set { _description = value; }
            }
            string _datatype;

            public string Datatype
            {
                get { return _datatype; }
                set { _datatype = value; }
            }
        }

        private void Search_SelectedValueChanged(object sender, EventArgs e)
        {
            SearchMethod();
        }

        private void SearchMethod()
        {
            DataTable dt = _gridEX.DataSource as DataTable;
            if (dt != null)
            {
                clsFilterItem it = Field.SelectedItem as clsFilterItem;
                if (it != null)
                {
                    if (dt.Columns.Contains(it.Code))
                    {
                        string search = "";
                        if (dt.Columns[it.Code].DataType == typeof(string))
                        {
                            switch (Search.Text)
                            {
                                case _EQ: search = it.Code + " = '" + Value.Text + "'"; break;
                                case _CO: search = it.Code + " LIKE '%" + Value.Text + "%'"; break;
                                case _EM: search = it.Code + " is null or " + it.Code + " = ''"; break;
                                case _EN: search = it.Code + " LIKE '%" + Value.Text + "'"; break;
                                case _BE: search = it.Code + " LIKE '" + Value.Text + "%'"; break;
                                case _G: search = it.Code + " > '" + Value.Text + "'"; break;
                                case _GE: search = it.Code + " >= '" + Value.Text + "'"; break;
                                case _L: search = it.Code + " < '" + Value.Text + "'"; break;
                                case _LE: search = it.Code + " <= '" + Value.Text + "'"; break;
                            }

                        }
                        else
                        {
                            if (Regex.IsMatch(Value.Text, @"[\d]"))
                                switch (Search.Text)
                                {
                                    case _BE:
                                    case _EN:
                                    case _EQ:
                                    case _CO: search = it.Code + " = " + Value.Text + ""; break;
                                    case _EM: search = it.Code + " is null"; break;
                                    case _G: search = it.Code + " > " + Value.Text + ""; break;
                                    case _GE: search = it.Code + " >= " + Value.Text + ""; break;
                                    case _L: search = it.Code + " < " + Value.Text + ""; break;
                                    case _LE: search = it.Code + " <= " + Value.Text + ""; break;
                                }
                        }
                        dt.DefaultView.RowFilter = search;
                        btnSearch.Text = "Clear";
                    }
                }
            }
        }

        private void Field_SelectedValueChanged(object sender, EventArgs e)
        {
            //Value.Text = "";
        }

        private void Value_KeyUp(object objSender, KeyEventArgs objArgs)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (btnSearch.Text == "Clear")
            {
                DataTable dt = _gridEX.DataSource as DataTable;
                if (dt != null)
                {
                    dt.DefaultView.RowFilter = "";
                    Value.Text = "";
                    btnSearch.Text = "Search";
                }
            }
            else
            {
                SearchMethod();
                //btnSearch.Text = "Clear";
            }
        }

        private void Value_TextChanged(object sender, EventArgs e)
        {
            SearchMethod();
        }
    }
}